package com.myschool.server.web.controller;

import java.sql.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.AttandenceManager;
import com.myschool.server.manager.GradeManager;
import com.myschool.server.manager.SectionManager;
import com.myschool.server.manager.UserManager;
import com.myschool.server.model.Attendance;
import com.myschool.server.model.Grade;
import com.myschool.server.model.Json;
import com.myschool.server.model.Rows;
import com.myschool.server.model.Section;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;
@Controller
public class AttandenceController {



	@Autowired
	UserManager userManager;
	
	@Autowired
	GradeManager gradeManager;

	@Autowired
	private SectionManager sectionManager;

	@Autowired
	private AttandenceManager attandenceManager;
	
	
	@RequestMapping("attendanceView.htm")
	public ModelAndView studentView() {

		Set<Grade> gradeList = gradeManager.retrieveAllGrades(1L);
		 ModelAndView view = new ModelAndView("AttendanceConfig");
		 view.addObject("gradeList",gradeList);
		 return view;
	}
	
	@RequestMapping("getAttandence.htm")
	public @ResponseBody
	Json createTeacher(HttpServletRequest request, String sidx, String sord,
			int page, int rows, Boolean _search, String searchField,
			String searchOper, String searchString) {

		Json jsonData = new Json();
		try
		{
			
			String section = request.getParameter("section");
			 String[] absentList = new String[0];
			 Long attendanceID = (long) 0;
			if(section == "")
				return jsonData;
			
			@SuppressWarnings("deprecation")
			Set<Attendance> att = attandenceManager.retriveAttandenceBySection(Long.valueOf(section),new Date(12, 12, 12) );
			
			if(att != null && att.size() > 0 )
			{
			Attendance c = (Attendance) att.toArray()[0];
		    attendanceID = c.getEntityId();
			String absents = c.getAbsentList(); 
			absentList = absents.split(",");
			}
			
			
			
			
			List<User> users= userManager.retrieveStudentBySection(Long.valueOf(section));

			int userCount = users.size();
			int rowsPerCol = userCount /3;
			int rowsFirstRow = rowsPerCol + (userCount - rowsPerCol*3);

			jsonData.page = page;
			jsonData.total = userCount;
			jsonData.record = userCount;

			jsonData.rows = new Rows[rowsFirstRow];
			 int absentnumber = absentList.length;
		 	int absentCount = 0;
 
			for (int count = 0; count < rowsPerCol; count++) 
			{
				User student1 = users.get(count);
				User student2 = users.get(count+rowsFirstRow);
				User student3 = users.get(count+rowsFirstRow+rowsPerCol);
				
				String StudentID1 = student1.getEntityId().toString();
				String StudentID2 = student2.getEntityId().toString();
				String StudentID3 = student3.getEntityId().toString();
				
				Boolean studentPresent1 = true;
				Boolean studentPresent2 = true;
				Boolean studentPresent3 = true;
				
				if(absentCount < absentnumber)
				for(String absentID : absentList)
				{
					if(absentID.trim().equals(StudentID1))
					{
						studentPresent1 = false;absentCount++;
						continue;
					}
					if(absentID.trim().equals(StudentID2))
					{
						studentPresent2 = false;absentCount++;
						continue;
					}
					if(absentID.trim().equals(StudentID3))
					{
						studentPresent3 = false;absentCount++;
						continue;
					}
					
				}
				
				Rows row = new Rows();
				row.i = count + 1;
				row.cell = new String[] { attendanceID.toString(),student1.getEntityId().toString(),
						student1.getFirstName() + " " + student1.getLastName(), studentPresent1.toString(), student2.getEntityId().toString(),
						student2.getFirstName() + " " + student2.getLastName(), studentPresent2.toString(),student3.getEntityId().toString(),
						student3.getFirstName() + " " + student3.getLastName(), studentPresent3.toString() };
				jsonData.rows[count] = row; 
			}
			
		/*	for(int count = rowsPerCol ; count < rowsFirstRow; count++)
			{
				
				User student1 = users.get(count);
				Rows row = new Rows();
				row.i = count + 1;
				row.cell = new String[] { "",student1.getEntityId().toString(),
						student1.getFirstName() + " " + student1.getLastName(), "1" };
				jsonData.rows[count] = row; 
				
			}
			String fistName = request.getParameter("section");*/

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

		return jsonData;
	}
	
	@RequestMapping("editAttandence.htm")
	public void deleteUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String attandenceID = request.getParameter("attendenceID");
			Boolean isAdd = false;
			 Attendance attendance = new Attendance() ;
			
			if(attandenceID.isEmpty() || attandenceID.equals("0"))
				isAdd = true;
			else
				attendance.setEntityId(Long.valueOf(attandenceID));
			
			String sectionID = request.getParameter("sectionID");
			String absentList = request.getParameter("attandenceValues");
			java.util.Date cal = new java.util.Date(2012, 3, 15);
          
            Section section = sectionManager.retrieve(Long.valueOf(sectionID));
			attendance.setSection(section);
			attendance.setAbsentList(absentList);
			attendance.setDate(cal);
			attandenceManager.create(attendance);
			
		} catch (Exception ex) {
			System.out.println(ex.getMessage()); 
		} 
	} 


	
}
